import { useMemberStore } from "@/stores";

const baseUrl = "https://pcapi-xiaotuxian-front-devtest.itheima.net"

const httpInterceptor = {
    //拦截器触发
    invoke(options: UniApp.RequestOptions){
        //非http开头，拼接上baseUrl
        if (!options.url.startsWith("http")){
            options.url = baseUrl + options.url;
        }
        //设置链接超时时间，默认60s
        options.timeout = 10000;
        //添加请求头部
        options.header = {
            ...options.header,
            "source-client": "miniapp"
        }
        //添加请求token
        const memberStore = useMemberStore();
        const token = memberStore.profile?.token;
        if (token){
            options.header.Authorization = token;
        }
        console.log(options);
    }
}

//拦截request请求
uni.addInterceptor("request", httpInterceptor);